<template>
    <div class="product-authorize" style="padding-left: 20px">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-row :gutter="20">
                <el-col :span="7">
                    <el-form-item label="设备编码" prop="serialNumber">
                        <el-input v-model="queryParams.serialNumber" placeholder="请输入设备编码"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="7">
                    <el-form-item label="授权码" prop="authorizeCode">
                        <el-input v-model="queryParams.authorizeCode" placeholder="请输入授权码"></el-input>
                    </el-form-item>

                </el-col>
                <el-col :span="7">
                    <el-form-item label="状态" prop="status">
                        <el-select v-model="queryParams.status">
                            <el-option v-for="dict in iot__authorize_status" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="padding-top: 2px;" :gutter="10">
                <el-col :span="1.5">
                    <el-button type="primary" @click="handleQuery()">搜索</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="info" :plain="true" @click="resetQuery">重置</el-button>
                </el-col>
            </el-row>
            <el-row style="padding-top: 15px;" :gutter="10">
                <el-col :span="1.5">
                    <el-button type="primary" :plain="true" @click="handleAdd"
                        v-hasPermi="['iot:productAuthorize:add']">生成授权码</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" :plain="true" @click="handleDelete()"
                        v-hasPermi="['iot:productAuthorize:remove']">批量删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="success" :plain="true" @click="handleExport"
                        v-hasPermi="['iot:productAuthorize:export']">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-link style="padding-top: 5px;" :underline="false">Tips：双击可以复制授权码。</el-link>
                </el-col>
            </el-row>
        </el-form>
        <el-table v-loading="loading" :data="productAuthorizeList" @cell-dblclick="copyAuthorizeCode" border
            style="width: 100%;padding-top: 8px;" small @selection-change="handleSelectionChange">
            <el-table-column type="selection" label="选择" width="55" />
            <el-table-column prop="authorizeId" label="授权码Id" align="center" v-if="false" />
            <el-table-column prop="authorizeCode" label="授权码" align="center" width="320" />
            <el-table-column prop="status" label="状态" width="100" align="center">
                <template #default="scope">
                    <dict-tag :options="iot__authorize_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column prop="deviceId" label="设备编号" />
            <el-table-column prop="date" label="授权时间" width="180" />
            <el-table-column prop="remark" label="备注" width="180" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
                <template #default="scope">
                    <el-button size="small" plain type="text" icon="user">设备授权</el-button>
                    <el-button size="small" plain type="text" icon="notebook">备注</el-button>
                    <el-button size="small" plain type="text" icon="delete"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
    <!--生成授权码对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px">
        <el-form ref="productAuthorizeFormRef" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="数量" prop="authorizeNum">
                <el-input-number v-model="form.authorizeNum" placeholder="请输入数量" style="width: 200px;" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>


</template>
<script lang="ts" setup name="ProductAuthorize">
import { listProductAuthorize, getProductAuthorize, delProductAuthorize, addProductAuthorize, updateProductAuthorize, addProductAuthorize1 } from '@/api/iot/productAuthorize';
import { ProductAuthorizeVO, ProductAuthorizeQuery, ProductAuthorizeForm, authorizeAddForm } from '@/api/iot/productAuthorize/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { iot__authorize_status } = toRefs<any>(proxy?.useDict('iot__authorize_status'));

const productAuthorizeList = ref<ProductAuthorizeVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const productAuthorizeFormRef = ref<ElFormInstance>();
const productIdAdd = inject<string>('productIdAdd')
const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
});
const dialog1 = reactive<DialogOption>({
    visible: false,
    title: ''
});


const initFormData: authorizeAddForm = {
    authorizeNum: 0,
    productId: productIdAdd
}
const data = reactive<PageData<authorizeAddForm, ProductAuthorizeQuery>>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined,
        authorizeCode: undefined,
        status: undefined,
        params: {
        }
    },
    rules: {
        authorizeNum: [
            { required: true, message: "数量必须大于0", trigger: "change" }
        ],
        productId: [
            { required: true, message: "产品ID不能为空", trigger: "blur" }
        ],
    }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询产品授权码列表 */
const getList = async () => {
    loading.value = true;
    const res = await listProductAuthorize(queryParams.value);
    productAuthorizeList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
    form.value = { ...initFormData };
    productAuthorizeFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
    console.log(typeof queryFormRef);
    queryFormRef.value?.resetFields();
    handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProductAuthorizeVO[]) => {
    ids.value = selection.map(item => item.authorizeId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "生成授权码";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: ProductAuthorizeVO) => {
    reset();
    const _authorizeId = row?.authorizeId || ids.value[0]
    const res = await getProductAuthorize(_authorizeId);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "修改产品授权码";
}

/** 提交按钮 */
const submitForm = () => {
    productAuthorizeFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            await addProductAuthorize1(form.value).finally(() => buttonLoading.value = false);
            proxy?.$modal.msgSuccess("操作成功");
            dialog.visible = false;
            // await getList();
        }
    });
}

/** 删除按钮操作 */
const handleDelete = async (row?: ProductAuthorizeVO) => {
    const _authorizeIds = row?.authorizeId || ids.value;
    debugger;
    await proxy?.$modal.confirm('是否确认删除产品授权码编号为"' + _authorizeIds + '"的数据项？').finally(() => loading.value = false);
    await delProductAuthorize(_authorizeIds);
    proxy?.$modal.msgSuccess("删除成功");
    await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
    proxy?.download('iot/productAuthorize/export', {
        ...queryParams.value
    }, `productAuthorize_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
    getList();
});

/**复制授权码 */
const copyAuthorizeCode = (row, column, cell, event) => {
    console.log(column.property);
    if (column.property == 'authorizeCode') {
        //复制成功提示"复制成功",复制失败提示"复制失败"
        navigator.clipboard.writeText(row.authorizeCode);
        proxy?.$modal.msgSuccess("复制成功");
    }




}


</script>

<style lang="css" scoped></style>